//首页课程集合
//课程里写的是collection -> courseCollection
const crawler = require('../../libs/crawler'),
      { crawlerConf } = require('../../config/config');

crawler({
  url: crawlerConf.url.main,
  callback () {
    const $ = window.$,
        $item = $('.agency-recommend-course');

    let data = [];
    
    $item.each((index, item) => {
      //jq的each和forEach参数相反
      const $el = $(item);
      const dataItem = {
        cid: index + 1, //把遍历的index+1作为id
        title: $el.find('.recommend-course-title span').eq(0).text().replace(/(\n|\s+|更多)/g, ''),
        info: $el.find('.rec-group-info').text(),
        qqQunLink: $el.find('.rec-group-join').prop('href'),
        posterUrl: $el.find('.rec-group-mask').css('background-image').match(/\"(.+?)\"/)[1],
        courseIdList: '',
        posterKey: ''
      }
      //查找课程id保存进list，关联所有课程页面
      let _idList = [];
      const $courseItem = $el.find('.course-card-item');
      $courseItem.each((index, item) => {
        const $elem = $(item);
        _idList.push($elem.find('.item-img-link').attr('data-id'));
      });
      dataItem.courseIdList = _idList.toString();
      data.push(dataItem);
    });
    return data;
  }
  //callback end
})